﻿using System;
using System.Windows.Forms;
using ExitGames.Client.Photon;
using PhotonServerCommon.Enums;
using PhotonTestClient.Handler;
using PhotonTestClient.Helper;

namespace PhotonTestClient.Photon
{
    public class PhotonListener : IPhotonPeerListener
    {
        public PhotonTestClient MasterWindow;

        public void DebugReturn(DebugLevel level, string message)
        {
            string debugString = "Level: " + level + " - Message: " + message;

            MasterWindow.SetStatusText(LogHelper.GetTimeStamp(), debugString);
        }

        public void OnOperationResponse(OperationResponse operationResponse)
        {
            var realmHandler = new RealmHandler() {OwningTestClient = MasterWindow};

            MasterWindow.SetStatusText(LogHelper.GetTimeStamp(),
                $"OnOperationResponse called with parameters: Code = {(OperationCodes) operationResponse.OperationCode} - SubCode = {(OperationSubCodes) operationResponse.Parameters[(byte) ParameterCodes.OperationSubCode]} - ReturnCode = {operationResponse.ReturnCode} - DebugMessage = {operationResponse.DebugMessage}");

            switch (operationResponse.OperationCode)
            {
                case (byte) OperationCodes.Transfer: realmHandler.HandleRealmMessage(operationResponse);
                    break;
                default:
                    break;
            }
        }

        public void OnStatusChanged(StatusCode statusCode)
        {
            MasterWindow.SetStatusText(LogHelper.GetTimeStamp(),
                $"OnStatusChanged called with parameters: StatusCode = {statusCode}");

            if (statusCode == StatusCode.Connect)
            {
                MasterWindow.PeerConnected = true;
                MasterWindow.PeerConnecting = false;
            }
            else if (statusCode == StatusCode.Disconnect)
            {
                MasterWindow.PeerConnected = false;
                MasterWindow.PeerConnecting = false;
            }
            else
            {
                MasterWindow.PeerConnected = true;
            }
        }

        public void OnEvent(EventData eventData)
        {
            MasterWindow.SetStatusText(LogHelper.GetTimeStamp(),
                $"OnEvent called with parameters: Code = {eventData.Code}");
        }
    }
}
